﻿#region Usings
using System;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

#endregion Usings

namespace MAQEmailGuidelines
{

    public partial class ThisAddIn
    {
        /// <summary>
        /// Trying to speed up load for this add-in
        /// </summary>
        /// <returns></returns>
        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return null;
        }       
        /// <summary>
        /// Perform check before sending an email
        /// </summary>		
        /// <param name="Item">Outlook Email Item</param>		
        /// <param name="Cancel">Cancel flag that will allow sending an email</param>		
        public void ItemSendEventHandler(object Item, ref bool Cancel)
        {
            try
            {
                if (Properties.Settings.Default.Enabled)
                {
                    ViolationCheckHelper checkViolation = null;
                    if (Item is Outlook.MeetingItem)
                    {
                        checkViolation = new ViolationCheckHelper(Item, MailErrors.MailType.Meeting);
                    }
                    else if (Item is Outlook.MailItem)
                    {
                        checkViolation = new ViolationCheckHelper(Item, MailErrors.MailType.Mail);
                    }
                    //spell checks
                    checkViolation.CheckSpellingMistakes();
                    //check if mail is directed to more than 1 recipient
                    checkViolation.CheckToCount();
                    //check if mail is being sent to someone in BCC
                    checkViolation.CheckBCC();
                    //check if mail body contains any exclamation marks
                    checkViolation.CheckExclamationMark();
                    //check if mail has a background image
                    checkViolation.CheckMailBackground();
                    //check if mail contains any slang
                    checkViolation.CheckForSlang();
                    //check if mail contains any uppercase words
                    checkViolation.HasAnyUpperCaseWords();
                    //check importance of mails
                    checkViolation.CheckMailImportance();
                    
                    foreach (string violation in MailErrors.ViolationList)
                    {
                        MailErrors.ViolationMessage += violation + "\n";
                    }
                    if (!string.IsNullOrEmpty(MailErrors.ViolationMessage))
                    {
                        DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(String.Format(Properties.Resources.ViolationMessage, MailErrors.ViolationMessage), Properties.Resources.PopupTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                        if (dialogResult == DialogResult.Yes)
                        {
                            MailErrors.ClearAllError();
                            //send email
                            Cancel = false;
                        }
                        else if (dialogResult == DialogResult.No)
                        {
                            MailErrors.ClearAllError();
                            //stop sending email
                            Cancel = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(Properties.Resources.ExceptionMessage, Properties.Resources.ExceptionPopupTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (dialogResult == DialogResult.Yes)
                {
                    MailErrors.ClearAllError();
                    //send email
                    Cancel = false;
                }
                else if (dialogResult == DialogResult.No)
                {
                    MailErrors.ClearAllError();
                    //stop send email
                    Cancel = true;
                }
            }

        }




        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            //this.Startup += new System.EventHandler(ThisAddIn_Startup);
            //this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(ItemSendEventHandler);
        }

        #endregion
    }
}

